(function() {
	var $tbody, $btm, $inputEl, $a, $page;
	$(function() {

		$tbody = $("#tbody");
		$btm = $("#btm").click(handleBtmClick).click();
		$inputEl = $("#inputEl");
		$a = $("#a").click(f2);
		$nav = $("#nav");
		$page = $("#page");
	});

	var handleBtmClick = function(evt) {
		evt.preventDefault();
		listAllByPage(1);
	}

	window.listAllByPage = function(pageNo) {
		var url = "/beadhouse/api/customer/selectAllByPage?pageNo=" + pageNo;
		$.get(url, function(resp) {
			if (resp.code == "ok") {

				f1(resp.data);
			}

		});
	};
	var f1 = function(data) {

		var html = "";
		var html1 = "";
		var page = data.pages;
		var data = data.list;
		console.log(page);
		if (data.length > 0) {
			for (var i = 0; i < data.length; i++) {
				html += '<tr>';
				html += '<td>' + i + '</td>';
				html += '<td>' + data[i].customerName + '</td>';
				if (data[i].customerSex) {
					html += '<td>男</td>';
				} else {
					html += '<td>男</td>';
				}
				html += '<td>' + data[i].customerAge + '</td> ';
				html += '	<td>' + data[i].recordId + '</td>';
				html += '	<td>徐伟</td>                                                                            ';
				html += '	<td>林杰</td>                                                                            ';
				html += '	<td>唐磊</td>                                                                            ';
				html += '	<td>'
						+ data[i].buildingId
						+ '</td>                                                                     ';
				html += '	<td>'
						+ data[i].bedId
						+ '</td>                                                                          ';
				html += '	<td>'
						+ data[i].checkinDate
						+ '</td>                                                                      ';
				html += '	<td>'
						+ data[i].expirationDate
						+ '</td>                                                                      ';
				html += '	<td>'
						+ data[i].psychosomaticState
						+ '</td>                                                                ';
				html += '	<td>三级</td>                                                                            ';
				html += '	<td>李柏</td>                                                                            ';
				html += '	<td>'
						+ data[i].contactTel
						+ '</td>                                                                     ';
				html += '	<td>生活半自理，需要人陪护</td>                                                          ';
				html += '	<td><a href="/beadhouse/api/nurfile/query?customerId='
						+ data[i].id
						+ '#'
						+ data[i].id
						+ '" ><span class="btn btn-success">查看</span></a></td> ';

				html += '	<td><a href="#"><span class="btn btn-success">查看</span></a></td>       ';
				html += '	<td>                                                                                     ';
				html += '		<a href="khxq.html" target="ckjkda"><button class="btn btn-warning">详情</button></a>';
				html += '	</td>                                                                                    ';
				html += '</tr>                                                                                  ';
			}
			html1 += '<ul class="pagination">';
			html1 += '	<li>                                               ';
			html1 += '		<a href="#" aria-label="Previous">             ';
			html1 += '			<span aria-hidden="true">&laquo;</span>    ';
			html1 += '		</a>                                           ';
			html1 += '	</li>                                              ';
			for (var j = 1; j <= page; j++) {
				html1 += '	<li><a href="javascript:listAllByPage(' + j + ')" >'
						+ j + '</a></li>                         ';
			}

			html1 += '	<li>                                               ';
			html1 += '		<a href="#" aria-label="Next">                 ';
			html1 += '			<span aria-hidden="true">&raquo;</span>    ';
			html1 += '		</a>                                           ';
			html1 += '	</li>                                              ';
			html1 += '</ul>	                                                   ';
		}

		else {
			html += '<tr>';
			html += '<td colspan="9999999">很抱歉没相关数据</td>'
			html += '</tr>';
		}
		$tbody.html(html);
		$nav.html(html1);
	};

	var f2 = function(evt) {
		var customerName = $inputEl.val();
		var url = "/beadhouse/api/customer/selectAllByPage?key=" + customerName;

		$.get(url, function(resp) {
			if (resp.code == "ok") {
				console.log(resp);
				f1(resp.data);
			}

		});
	}
})();
